package com.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.CountDownLatch;

/**
 * @description:
 * @projectName: hadoopstudy
 * @see: com.zookeeper
 * @author: 崔宗祺
 * @createTime: 2024/5/10 14:13
 * @version: 1.0
 */
public class ZookeeperStudy {
    private CountDownLatch singal = new CountDownLatch(1);

    public ZooKeeper getZk() throws Exception {
        ZooKeeper zk = new ZooKeeper("192.168.10.11:2181", 3000, x -> {
            if (x.getState() == Watcher.Event.KeeperState.SyncConnected) {
                singal.countDown();
            }
        });
        singal.await();
        return zk;
    }

    public void createNode(String path, String data) throws Exception {
        ZooKeeper zk = getZk();
        String file_path = zk.create(path,
                data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(file_path);
        zk.close();
    }

    public void createSyncNode(String path, String data) throws Exception {
        ZooKeeper zk = getZk();
        AsyncCallback.StringCallback as = new AsyncCallback.StringCallback() {
            @Override
            public void processResult(int i, String s, Object o, String s1) {
                switch (KeeperException.Code.get(i)) {
                    case OK:
                        System.out.println("创建节点成功");
                        System.out.println(i + "*" + s + "*" + o + "*" + s1);
                        break;
                    case NODEEXISTS:
                        System.out.println("节点不存在");
                        break;
                    case CONNECTIONLOSS:
                        System.out.println("连接失败");
                        break;
                    default:
                        System.out.println("其他");
                }
            }
        };
        zk.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT, as, "hello");
    }
    public void getData(String path) throws Exception {
        ZooKeeper zk=getZk();
        Stat s=new Stat();
        byte[] data=zk.getData(path,false,s);
        System.out.println(new String(data));
    }
    public void getSyncData(String path) throws Exception {
        ZooKeeper zk=getZk();
        Stat s=new Stat();
        AsyncCallback.DataCallback ad=new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int i, String s, Object o, byte[] bytes,
                                      Stat stat) {
                if(KeeperException.Code.get(i)== KeeperException.Code.OK){
                    System.out.println(s+"--"+o+"--"+new String(bytes)+"--"+stat);
                }
            }
        };
        zk.getData(path,false,ad,"args");
    }
    public void setData(String path,String data) throws Exception {
        ZooKeeper zk=getZk();
        Stat s=zk.setData(path,data.getBytes(),-1);
        System.out.println(s);
    }
    public void setSyncData(String path,String data) throws Exception {
        ZooKeeper zk=getZk();
        AsyncCallback.StatCallback as=new AsyncCallback.StatCallback() {
            @Override
            public void processResult(int i, String s, Object o, Stat stat) {
                if(KeeperException.Code.get(i)== KeeperException.Code.OK){
                    System.out.println(s+"--"+o+"--"+stat);
                }
            }
        };
        zk.setData(path,data.getBytes(),-1,as,"args");
    }
    public static void main(String[] args) throws Exception {
//        new ZookeeperStudy().getZk();
        new ZookeeperStudy().createNode("/htl","htl");
        new ZookeeperStudy().createSyncNode("/fyy","/fyy");
    }
}
